#include <DynamicArray.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
#include "Scene.h"
#include "player.h"
#include "monster.h"

bool UseProp()
{

}

bool Battle(player *p,monster *m)
{
    printf("你遭遇到了|%s|,  %3d❤️  %2d⚔️\n",m->name.string,m->HP,m->ATK);
    //让程序休眠1s
    sleep(1);
    while(1)
    {
        //清屏
        system("clear");
        printf("\n--------------------战斗状态----------------------\n");
        PrintPlayer(p);
        printf("敌方信息:💀 名称:|%s| %3d❤️  %2d⚔️\n",m->name.string,m->HP,m->ATK);
        printf("\n--------------------战斗选择----------------------\n");
        printf("1.进攻⚔️\n");
        printf("2.暴击💥\n");
        printf("3.逃跑🏳️\n");
        printf("4.使用道具💼\n");
        printf("--------------------------------------------------\n");
        printf("请进行你的选择:");
        int choice = 0;
        while(getchar() != '\n');
        scanf("%d",&choice);
        switch (choice)
        {
            case 1:
                printf("你对|%s|发起了进攻⚔️ ,造成了%d点伤害\n",m->name.string,p->ATK);
                m->HP -= p->ATK;
                if(m->HP > 0)
                {
                    printf("|%s|对你发起了反击⚔️ ,造成了%d点伤害\n",m->name.string,m->ATK);
                    p->HP -= m->ATK;
                }
                break;
            case 2:
                if(1)
                //if(GetRand(2) == 1)
                {
                    printf("你对|%s|发起了暴击💥 ,造成了%d点伤害\n",m->name.string,p->ATK*2);
                    m->HP -= 2 * p->ATK;
                    if(m->HP > 0)
                    {
                        printf("|%s|对你发起了反击⚔️ ,造成了%d点伤害\n",m->name.string,m->ATK);
                        p->HP -= m->ATK;
                    }                   
                }
                else
                {
                    printf("你暴击失败,未造成伤害\n");
                    printf("|%s|对你发起了反击⚔️ ,造成了%d点伤害\n",m->name.string,m->ATK);
                    p->HP -= m->ATK;
                }
                break;
            case 3:
                if(IsEqual(&m->name,"黑龙🐉") == true)
                {
                    printf("BOSS战,无法逃跑!\n");
                    printf("|%s|对你发起了攻击⚔️ ,造成了%d点伤害\n",m->name.string,m->ATK);
                    p->HP -= m->ATK;
                    break;
                }
                if(GetRand(2) == 1)
                {
                    printf("🎌 你成功逃跑了!\n");
                    return true; //逃离战斗，存活
                }
                else
                {
                    printf("你逃跑失败!");
                    printf("|%s|对你发起了攻击⚔️ ,造成了%d点伤害\n",m->name.string,m->ATK);
                    p->HP -= m->ATK;
                }
                break;
            case 4:
                ShowBag(p);
                sleep(2);

                break;
            default:
                break;
        }
        sleep(1);
        if(p->HP <= 0)
        {
            p->HP = 0;
            printf("战斗失败!\n");
            return false;
        }
        if(m->HP <= 0)
        {
            m->HP = 0;
            printf("|%s|被你击败了!掉落了%d🪙\n",m->name.string,m->golden);
            p->exp += m->exp;
            p->golden += m->golden;
            sleep(1);
            return true;
        }
    }  
}



int main()
{
    //初始化场景
    char map[MAPSZIE][MAPSZIE] = {0};
    InitMap(map);

    //初始化玩家
    player p;
    InitPlayer(&p);
    map[p.x][p.y] = p.symbol;
    

    //初始化怪兽
    INITDARRAY(monsters);
    SetMosters(&monsters);
    SetMonsterPosition(&monsters,map);

    PrintMap(map);
    monster *posM = NULL;
    //移动玩家
    while(1)
    {
        PrintPlayer(&p);
        PlayerMove(&p,map);
        if(posM != NULL)
        {
            map[posM->x][posM->y] = posM->symbol;
            posM = NULL;
        }
        monster *m = GetMonsterByPosition(&monsters,p.x,p.y);
        if(m != NULL)
        {
            if(Battle(&p,m) == false)
            {
                printf(" 你嘎了!游戏结束!☠️\n");
                sleep(1);
                return -1;
            }

            LevelUp(&p);

            if(m->HP == 0)
            {
                if(IsEqual(&m->name,"黑龙🐉") == true)
                {
                    printf("🎉你击败了BOSS,游戏结束!🎉\n");
                    return 0;
                }
                map[m->x][m->y] = p.symbol;
            }
            else
                posM = m;
        }
        PrintMap(map);
    }

    return 0;
}